package com.moorror.codegenvel.common.table;

import com.intellij.icons.AllIcons;
import com.intellij.ui.ColoredTableCellRenderer;
import com.intellij.ui.dualView.TreeTableView;
import com.moorror.codegenvel.common.utils.JacksonUtils;

import javax.swing.*;

/**
 * @Author jiyanghuang
 * @Date 2022/8/3 23:35
 */
public class MRTableCellRenderer extends ColoredTableCellRenderer {

    /**
     * @param table    表格对象
     * @param value    某个方格的值,可以根据不同的类型拼接
     * @param selected 当前是否被选中
     * @param hasFocus 是否获取焦点
     * @param row      行 0 开始
     * @param column   列 0 开始
     */
    @Override
    protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
        if (value == null) {
            append("null", StyleAttributesProvider.getNullAttribute());
        } else {
            if (value instanceof Number) {
                append(String.valueOf(value), StyleAttributesProvider.getNumberAttribute());
            } else if (value instanceof Boolean) {
                append(String.valueOf(value), StyleAttributesProvider.getBooleanAttribute());
            } else if (value instanceof ColumnDesc) {
                ColumnDesc val = (ColumnDesc) value;
//                System.out.println("row:" + row + " column:" + column + " value:" + String.valueOf(val.getValueShow()));
                if (null != val.getColumnShowAttr() && !val.getColumnShowAttr().equals(ColumnShowAttr.NULL)) {
                    append(String.valueOf(val.getValueShow()), val.getColumnShowAttr().getSimpleTextAttributes());
                } else {
                    if (JacksonUtils.jsonStr(val.getValueShow())) {
                        append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getKeyValueAttribute());
                    } else {
                        if(ColumnValueEnum.SELECT.equals(val.getColumnValueEnum())){
                            append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                            return;
                        } else if(ColumnValueEnum.SELECT_MULTI.equals(val.getColumnValueEnum())){
                            append(null == val.getNameShow() ? "" : String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                            return;
                        }
                        append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                    }
                }
            } else {
                append(String.valueOf(value), StyleAttributesProvider.getStringAttribute());
            }
        }
    }

    /**
     * 如果遇到树形转换时
     * @param table
     * @param value
     * @param selected
     * @param hasFocus
     * @param row
     * @param column
     */
    protected void customizeCellRendererTree(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
        if (table instanceof TreeTableView) {
            TreeTableView tableView = (TreeTableView) table;
            int modelRow = table.convertRowIndexToModel(row);
            boolean expanded = tableView.getTree().isExpanded(modelRow);
//            setIconTextGap(10);
            appendTextPadding(20);
            if (expanded) {
                setIcon(AllIcons.General.ChevronDown);
            } else {
                setIcon(AllIcons.General.ChevronRight);
            }

            appendTextPadding(10);
            System.out.println("--treeExpanded--------------------->" + expanded);

            if (value == null) {
                append("null", StyleAttributesProvider.getNullAttribute());
            } else {
                if (value instanceof Number) {
                    append(String.valueOf(value), StyleAttributesProvider.getNumberAttribute());
                } else if (value instanceof Boolean) {
                    append(String.valueOf(value), StyleAttributesProvider.getBooleanAttribute());
                } else if (value instanceof ColumnDesc) {
                    ColumnDesc val = (ColumnDesc) value;
//                System.out.println("row:" + row + " column:" + column + " value:" + String.valueOf(val.getValueShow()));
                    if (null != val.getColumnShowAttr() && !val.getColumnShowAttr().equals(ColumnShowAttr.NULL)) {
                        append(String.valueOf(val.getValueShow()), val.getColumnShowAttr().getSimpleTextAttributes());
                    } else {
                        if (JacksonUtils.jsonStr(val.getValueShow())) {
                            append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getKeyValueAttribute());
                        } else {
                            if (ColumnValueEnum.SELECT.equals(val.getColumnValueEnum())) {
                                append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                                return;
                            } else if (ColumnValueEnum.SELECT_MULTI.equals(val.getColumnValueEnum())) {
                                append(null == val.getNameShow() ? "" : String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                                return;
                            }
                            append(String.valueOf(val.getValueShow()), StyleAttributesProvider.getStringAttribute());
                        }
                    }
                } else {
                    append(String.valueOf(value), StyleAttributesProvider.getStringAttribute());
                }
            }
        }
    }
}
